
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author theInterloper
 */
public class menu {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        HashSet books = new HashSet();
        
        books.add ("Hocus Pocus, 947184628");
        books.add ("Cats Cradle, 681530956");
        books.add ("Timequake, 184967264");
        books.add ("Breakfast of Champions, 714509525");
        books.add ("The Sirens of Titan, 902486334");
        books.add ("Pearls Before Swine, 327163523");
        
        
        
        // TODO code application logic here
        int option;
            do{
                
                Scanner input = new Scanner(System.in);

                System.out.println("1.  Add Book");
                System.out.println("2.  Display All Books");
                System.out.println("3.  Sort by ISBN");
                System.out.println("4.  Find by ISBN");
                System.out.println("5.  Sort by name");
                System.out.println("0.  Exit");
                System.out.println("Enter Option:");
                
                option = input.nextInt();


                    switch(option){
                        case 0: break;
                        case 1:
                            addBook(books);
                        break;
                        case 2:
                            showAll(books);
                        break;
                        case 3:
                            sortISBN(books);
                        break;
                        case 4:
                            findISBN(books);
                        break;
                        case 5:
                            sortName(books);
                        break;
                        default:
                            System.out.println("not a valid option");
                            break;
                    }
                }while(option!=0);
    }
    
    private static void sortName(HashSet books) {
        Set booksByName = new TreeSet(books);
        
        System.out.print( "Books sorted by name are: \n" );
      
        Iterator iterator = booksByName.iterator();
        while (iterator.hasNext())
            System.out.print( iterator.next() + "\n" );
        System.out.println();
        
    }

    private static void findISBN(HashSet books) {
        Scanner input = new Scanner(System.in); 
        System.out.print( "Please enter an ISBN:   " );
        int bookISBNentered = Integer.parseInt(input.nextLine());
        Iterator iterator = books.iterator();
        String split = "";
        while (iterator.hasNext()){
            split = (iterator.next().toString()) ;
            StringTokenizer st = new StringTokenizer(split, ",");
            String bookName = st.nextToken();
            String bookISBN = st.nextToken();
            int bookISBNnum = Integer.parseInt(bookISBN);
            if(bookISBNentered == bookISBNnum){
                System.out.print("The corresponding book is: " + bookName);
            }
        }
    }

    private static void sortISBN(HashSet books) {
        Set booksByISBN = new TreeSet();
        Iterator iterator = books.iterator();
        String split ="";
        while (iterator.hasNext()){
            split = (iterator.next().toString()) ;
            StringTokenizer st = new StringTokenizer(split, ",");
            String bookName = st.nextToken();
            String bookISBN = st.nextToken();
            
            booksByISBN.add(bookISBN + ", " + bookName);
        }
        printTreeSet(booksByISBN);
    }

    private static void addBook(HashSet books) {
        Scanner input = new Scanner(System.in); 
        System.out.print("Please enter the name of a book:   ");
        String bookName = input.nextLine();
        System.out.print("Please enter an ISBN:   ");
        String bookISBN = input.nextLine();
        books.add(bookName + ", " + bookISBN);
      
    }

    private static void showAll(HashSet books) {
        printHashSet(books);
    }
    
     public static void printHashSet(HashSet books) {
        System.out.print( "Books in store are: \n" );
        Iterator iterator = books.iterator();
        while (iterator.hasNext())
            System.out.print( iterator.next() + "\n" );
        System.out.println();
    }
     
    public static void printTreeSet(Set books) {
        System.out.print( "Books in store are: \n" );
        Iterator iterator = books.iterator();
        while (iterator.hasNext())
            System.out.print( iterator.next() + "\n" );
        System.out.println();
    }

}
